<template>
  <el-dialog
    v-model="showDialog"
    title="下载申请"
    left
    :append-to-body="true"
    class="drag-dialog small-padding-dialog"
    width="80%"
    :before-close="closeDialog"
    :close-on-click-modal="false"
    destroy-on-close
    align-center
  >
    <el-form :model="form" label-width="120px" ref="formRef">
      <el-row justify="start">
        <el-col :span="24">
          <el-form-item
            label="下载目的"
            :rules="{ required: true, trigger: ['blur', 'change'], message: '请填写下载目的' }"
            prop="objective"
          >
            <el-input type="textarea" v-model="form.objective" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="下载用途"
            :rules="{ required: true, trigger: ['blur', 'change'], message: '请填写下载用途' }"
            prop="useTo"
          >
            <el-input type="textarea" v-model="form.useTo" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="水印" prop="watermark">
            <el-select v-model="form.watermark" placeholder="请选择水印">
              <el-option
                v-for="item in markList"
                :key="item.id"
                :label="item.text"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="name">
            <el-input type="textarea" v-model="form.remark" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="下载信息">
            <el-table v-if="type == 0" :data="list" style="width: 100%" highlight-current-row>
              <el-table-column align="center" prop="id" label="ID" />
              <el-table-column align="center" prop="name" label="名称" />
              <el-table-column align="center" prop="ext" label="格式" />
              <el-table-column align="center" prop="creator" label="创建人" />
              <el-table-column align="center" prop="size" label="大小" />
              <el-table-column align="center" prop="createTime" label="创建日期" />
            </el-table>
            <el-table v-if="type == 1" :data="list" stripe style="width: 100%">
              <el-table-column label="ID" prop="id" align="center" />
              <el-table-column label="图片" prop="url" align="center">
                <template #default="scope">
                  <el-image :src="scope.row.url" style="max-width: 50px">
                    <template #error>
                      <div class="image-slot">
                        <el-icon>
                          <icon-picture />
                        </el-icon>
                      </div>
                    </template>
                  </el-image>
                </template>
              </el-table-column>
              <el-table-column label="名称" prop="name" align="center" />
              <el-table-column label="格式" prop="format" align="center" />
              <el-table-column label="大小" prop="fileSize" align="center" />
            </el-table>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="checkCopyright()">
          <Icon icon="ep:edit" />
          查看版权信息
        </el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import * as downApi from '@/api/down'
import * as watermarkApi from '@/api/watermark'
import * as CopyrightApi from '@/api/copyright/copyright'
import router from '@/router'
const markList = ref([])
const props = defineProps({
  metaId: {
    default: '',
    type: String
  },
  type: {
    default: -1,
    type: Number
  },
  columns: {
    default: null,
    type: Array
  },
  list: {
    default: null,
    type: Array
  }
})
const type = ref(0)
const showDialog = ref(false)
const openDialog = async (t) => {
  type.value = t
  showDialog.value = true
  markList.value = await watermarkApi.list()
}
const closeDialog = () => {
  form.value = {}
  showDialog.value = false
}
const form = ref({})
function isValidJson(str) {
  try {
    JSON.parse(str)
    return true
  } catch (e) {
    return false
  }
}
/** 查看版权信息 */
const checkCopyright = async () => {
  let resource = props.list[0]
  if (resource.type === 10) {
    // 图片
    resource.type = 43
  } else if (resource.type === 48) {
    // 视频
    resource.type = 48
  } else if (resource.type === 51) {
    // 音频
    resource.type = 51
  } else if (resource.type === 42) {
    // 图书
    resource.type = 42
  }
  console.log(resource)
  await CopyrightApi.getCopyrightResourcesInfo(resource.id, resource.type).then((res) => {
    console.log(res)
    if (res) {
      router.push({
        path: '/copyright/updateCopyRight',
        query: {
          type: '查看',
          id: res.copyrightId
        }
      })
    } else {
      ElMessage.warning('该资源未查到版权信息!')
    }
  })
}
function isNumber(str: string): boolean {
  return /^\d+$/.test(str) // 只包含数字的正则表达式
}
const imaged = (row) => {
  if (isNumber(row)) {
    return false
  }
  return isValidJson(row)
}
const buildImage = (name, row) => {
  if (row[name] && row[name] != 'null') {
    return JSON.parse(row[name].replaceAll('\\', ''))[0].url
  } else {
    return ''
  }
}
const save = async () => {
  form.value.type = props.type
  form.value.metaId = props.metaId
  let ids = []
  let data = props.list
  data.forEach((item) => {
    ids.push(item.id)
  })
  form.value.data = ids
  await downApi.application(form.value)
  ElMessage.success('下载申请成功，请等待审核!')
  closeDialog()
}
defineExpose({
  openDialog
})
</script>
